package com.cskaoyan.aspect;

import com.cskaoyan.bean.pojo.MallAd;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.pojo.MallLog;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.mapper.MallLogMapper;
import com.cskaoyan.service.LogService;
import org.apache.catalina.core.JreMemoryLeakPreventionListener;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author: Fengyi
 * @description: 日志记录
 * @time: 2021/7/9 20:48
 */
@Aspect
@Component
public class CustomAspect {

    @Autowired
    LogService logService;

    @Pointcut("@annotation(com.cskaoyan.anno.LogAnnotation)")
    public void controllerPointcut() {
    }


    @Around("controllerPointcut()")
    public BaseRespVo<Object> methodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes attributes = requestAttributes;
        HttpServletRequest request = attributes.getRequest();
        proceedingJoinPoint.getSourceLocation();
        //session域
        HttpSession session = request.getSession();
        //ip地址
        String remoteAddr = request.getRemoteAddr();

        //创建MallLog对象，用于向数据库存数据
        MallLog mallLog = new MallLog();

//        mallLog.setAdmin("admin123");
        mallLog.setIp(remoteAddr);
        mallLog.setType(1);

        Subject subject = SecurityUtils.getSubject();


        Signature signature = proceedingJoinPoint.getSignature();
        String methodName = signature.getName();
        MallAdmin admin = null;

        // TODO 与logout对应起来  服务器重开的情况下  判空  直接返回json字符串
//        if("logout".equals(methodName)){


        if("logout".equals(methodName)) {
            admin = (MallAdmin) subject.getPrincipals().getPrimaryPrincipal();
        }

        BaseRespVo<Object> proceed = (BaseRespVo<Object>) proceedingJoinPoint.proceed();

        if(!"logout".equals(methodName)) {
            admin = (MallAdmin) subject.getPrincipals().getPrimaryPrincipal();
        }

        if("login".equals(methodName)) {

        }
        mallLog.setAdmin(admin.getUsername());


        if ("login".equals(methodName)) {
            mallLog.setAction("登录");
            mallLog.setStatus(true);
            if (proceed.getErrno() == 605) {
                mallLog.setStatus(false);
                mallLog.setAdmin("匿名用户");
                mallLog.setResult(proceed.getErrmsg());
            }
        } else if ("logout".equals(methodName)) {
            mallLog.setAction("登出");
            mallLog.setStatus(true);
        } else if ("createAdmin".equals(methodName)) {
            mallLog.setAction("添加管理员");

            mallLog.setStatus(false);
            mallLog.setResult(proceed.getErrmsg());
            if (proceed.getErrno() == 0) {
                mallLog.setStatus(true);
                MallAdmin data = (MallAdmin) proceed.getData();
                mallLog.setResult(data.getUsername());
            }
        } else if ("updateAdmin".equals(methodName)) {
            mallLog.setAction("编辑管理员");
            mallLog.setStatus(false);
            mallLog.setResult(proceed.getErrmsg());
            if (proceed.getErrno() == 0) {
                mallLog.setStatus(true);
                MallAdmin data = (MallAdmin) proceed.getData();
                mallLog.setResult(data.getUsername());
            }
        } else if ("deleteAdmin".equals(methodName)) {
            mallLog.setAction("删除管理员");
            //通过方法参数获取username
            Object[] args = proceedingJoinPoint.getArgs();
            MallAdmin mallAdmin = (MallAdmin) args[0];
            mallLog.setResult(mallAdmin.getUsername());
            mallLog.setStatus(true);
        }


        Date date = new Date();
        mallLog.setUpdateTime(date);
        mallLog.setAddTime(date);


        logService.insertLog(mallLog);

        return proceed;
    }
}



